package jdbc;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;


import javax.mail.search.AndTerm;



public class Email {
	private static final String smtp_host = "smtp.sina.com";
	private static final String pop3_host = "pop3.sina.com";
	private static final String home_address = "chensi_123789@sina.com";
	private static final String password = "cs123456";
	
	public static void main(String[] args) throws SQLException{
		Connection ssss = null;
		try {
			ssss = database.getConnection();
			String[] addresses = database.searchEmailAddress(ssss);
			for(int i = 0; i < addresses.length; i++){
			Email.sendMail(addresses[i]);
				}
			analysis(receiveMail());
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	//发邮件
	public static void sendMail(String target_address){
		Properties properties = System.getProperties();
		properties.put("mail.smtp.host", smtp_host);
		properties.put("mail.smtp.auth", true);
		Session session = Session.getDefaultInstance(properties, new Authenticator(){
			@Override
			public PasswordAuthentication getPasswordAuthentication(){
				return new PasswordAuthentication(home_address, password);
			}
			
		});
		try{
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(home_address));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(target_address));
			message.setSubject("java");
			message.setText("DO you like me ?");
			Transport.send(message);
			
		}catch(MessagingException e){
			e.printStackTrace();
		}
	}
	
	//收邮件
	public static Message[] receiveMail(){
		Properties properties = System.getProperties();
		properties.put("mail.pop3.ssl.enable", true);
		properties.put("mail.pop3.host", pop3_host);
		properties.put("mail.pop3.port", 995);
		
		Session session = Session.getInstance(properties, null);
		Message [] messages = null;
		Store store = null;
		Folder folder = null;
		
		try{
		    store = session.getStore("pop3");    //Store代表存储邮件的邮件服务器
		    store.connect(pop3_host, home_address, password);
		    folder = store.getFolder("INBOX");
		    folder.open(Folder.READ_ONLY);
//		    messages = folder.getMessages();
		    SearchTerm st = new AndTerm(new SubjectTerm("java"),new SubjectTerm("chensi"));
		    messages = folder.search(st);
		    //显示邮件信息
			for(int i = 0; i < messages.length; i++) {  
			
				String subject = messages[i].getSubject();  
				String from = (messages[i].getFrom()[0]).toString();
		              
				System.out.println("第 " + (i+1) + "封邮件的主题：" + subject);  
				System.out.println("第 " + (i+1) + "封邮件的发件人地址：" + from);  
				
			}
		   
		} catch(Exception e){
			e.printStackTrace();
		}

		return messages;
	}
	 
	public static void analysis(Message[] messages) throws IOException{
		String a = "yes";
		String b = "No";
		for(int i=0;i<messages.length; i++){
			String from;
			try {
				from = (messages[i].getFrom()[0]).toString();

				String c = messages[i].getSubject();
				if(c.contains("yes")){
					System.out.println(from + "likes you");
				}
				if(c.contains("no")){
					System.out.println(from + "doesn't like you");
				} 
					}catch (MessagingException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			
			}
		}
}
	

	
